package com.qingcloud.adminbackend.controller;


import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.aline.*;
import com.qingcloud.adminbackend.entity.aline.*;
import com.qingcloud.adminbackend.service.ISysSensorAlineAitectServiceNew;
import com.qingcloud.adminbackend.utils.CookieUtil;
import com.qingcloud.adminbackend.utils.PoiExcelExport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 传感器数据校准表(室内设备) 前端控制器
 * </p>
 *
 * @author liuziyong
 * @since 2019-04-11
 */
@RestController
@RequestMapping("/sys/sensor/aline/aitect")
@Api(value = "室内新业务参数校准API", description = "室内新业务参数校准API")
public class SysSensorAlineAitectController {

    @Value("${file.rootpath}")
    private String ROOTPATH;

    @Resource
    private ISysSensorAlineAitectServiceNew sysSensorAlineAitectService;

    @ApiOperation(value = "参数校准分页查询", notes = "参数校准分页查询", response = AlinePageAitectVO.class)
    @PostMapping("/findPage")
    public AjaxResult findPage(@RequestBody AlinePageDTO alinePageDTO, HttpServletRequest request) {
        alinePageDTO.setUserId(CookieUtil.getUserId(request));
        alinePageDTO.setIsAdmin(CookieUtil.isAdmin(request));
        return new AjaxResult(WamingConstant.SUCCESS, null, sysSensorAlineAitectService.findPage(alinePageDTO));
    }

    @ApiOperation(value = "参数校准数据导出", notes = "参数校准数据导出", response = AlinePageAitectVO.class)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody AlinePageDTO alinePageDTO,
                             HttpServletRequest request) throws Exception {
        alinePageDTO.setUserId(CookieUtil.getUserId(request));
        alinePageDTO.setIsAdmin(CookieUtil.isAdmin(request));
        String fileName;
        String sheetName;
        if (0 == alinePageDTO.getHisFlag()) {
            fileName = "CalibrationData" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".xlsx";
            sheetName = "参数校准";
        } else {
            fileName = "CalibrationDataHistory" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".xlsx";
            sheetName = "参数校准历史";
        }
        List<AlinePageAitectVO> alinePageAitectVOList = sysSensorAlineAitectService.find(alinePageDTO);
        String[] titleName = {"设备编号", "监测因子", "传感器状态", "最小浓度值", "最大浓度值", "参数K", "参数B", "操作", "操作结果", "操作时间"};// 列名
        String[] titleColumn = {"equipmentId", "factorName", "sensorStatusName", "startValue", "endValue", "paramK", "paramB", "operateFlagName", "operateStatusName", "operateTime"};
        int[] titleSize = {15, 10, 12, 12, 12, 12, 12, 10, 10, 22};

        String fileDir = ROOTPATH + "/temp";
        File dir = new File(fileDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String file = fileDir + "/" + new String(fileName.getBytes(), "ISO8859-1");

        PoiExcelExport poiExcelExport = new PoiExcelExport(file, sheetName);
        poiExcelExport.wirteExcel(titleColumn, titleName, titleSize, alinePageAitectVOList);

        AjaxResult result = new AjaxResult();
        String url = request.getContextPath() + "/file/view/temp/" + fileName;
        Map<String, String> resultMap = new HashMap<>(1);
        resultMap.put("url", url);
        result.setResult(resultMap);
        result.setErroCode(2000);
        return result;
    }

    @ApiOperation(value = "基础指令参数设置下发", notes = "基础指令参数设置下发")
    @PostMapping("/command/set")
    public AjaxResult setCommand(@RequestBody BaseSetCommandDTO baseSetCommandDTO) {
        return sysSensorAlineAitectService.setCommand(baseSetCommandDTO);
    }

    @ApiOperation(value = "MQTT登录信息参数设置下发", notes = "MQTT登录信息参数设置下发")
    @PostMapping("/command/setMqtt")
    public AjaxResult setMqtt(@RequestBody MqttSetCommandDTO mqttSetCommandDTO) {
        return sysSensorAlineAitectService.setMqttCommand(mqttSetCommandDTO);

    }

    @ApiOperation(value = "基础指令查询下发", notes = "基础指令查询下发")
    @PostMapping("/command/query")
    public AjaxResult queryCommand(@RequestBody BaseQueryCommandDTO baseQueryCommandDTO) {
        return sysSensorAlineAitectService.queryCommand(baseQueryCommandDTO);
    }

    @ApiOperation(value = "KB值设置下发", notes = "KB值设置下发")
    @PostMapping("/command/setKb")
    public AjaxResult setKb(@RequestBody List<AitectKbDTO> aitectKbDTOList) {
        return sysSensorAlineAitectService.setKb(aitectKbDTOList);
    }

    @ApiOperation(value = "查询KB值批量查询下发(模板导入)", notes = "查询KB值批量查询下发(模板导入)")
    @PostMapping("/command/importQueryKb")
    public AjaxResult batchQueryKb(@RequestParam("excelFile") MultipartFile excelFile, HttpServletRequest request) throws Exception {
        String userId = CookieUtil.getUserId(request);
        Boolean isAdmin = CookieUtil.isAdmin(request);
        return sysSensorAlineAitectService.importQueryKb(excelFile, userId, isAdmin);
    }

    @ApiOperation(value = "参数KB值批量设置下发(模板导入)", notes = "参数KB值批量设置下发(模板导入)")
    @PostMapping("/command/importSetKb")
    public AjaxResult batchSetKb(@RequestParam("excelFile") MultipartFile excelFile, HttpServletRequest request) {
        String userId = CookieUtil.getUserId(request);
        Boolean isAdmin = CookieUtil.isAdmin(request);
        return sysSensorAlineAitectService.importSetKb(excelFile, userId, isAdmin);
    }

}

